package com.hzdaba.aiapi.controller;

import com.hzdaba.aiapi.vo.AiApiResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 处理所有错误请求
 *
 * @author 刘逸晖
 */
@RestController
@RequestMapping("/error")
public class ErrorController {

    /**
     * 请求中存放错误信息的 key
     */
    @Value("${aiapi.request.attribute.error.key}")
    private String keyOfError;

    /**
     * 处理具体的错误
     *
     * @param request 请求
     * @return 错误响应
     */
    @RequestMapping("/error")
    public AiApiResponse handleError(HttpServletRequest request) {
        return AiApiResponse.fail((String) request.getAttribute(this.keyOfError));
    }

}
